package com.jcl.recruit.login.service.impl;

import com.jcl.core.security.SecurityUtils;
import com.jcl.recruit.corp.logic.SubcompanyLogic;
import com.jcl.recruit.corp.model.Subcompany;
import com.jcl.recruit.user.logic.UserLogic;
import com.jcl.recruit.user.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jcl.recruit.login.service.LoginService;

import java.util.Date;

/**
 * @ClassName LoginService
 * @Description TODO
 * @Author yyliu
 * @Date 2018/10/22 11:42
 * @Version 1.0
 **/
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    protected UserLogic userLogic;

    @Autowired
    protected SubcompanyLogic subcompanyLogic;

    /* *
     * @Author yyliu
     * @Description //TODO
     * @Date 10:10 2018/9/14
     * @Param [user]
     * @return void
     **/
    @Override
    public User permissAlert(User user){

        Subcompany subcompany = subcompanyLogic.findOneByCode(user.getCorpCode());
        Date abateDate = subcompany.getAbateDate();
        String token = SecurityUtils.encodeUserToken(user.getCorpCode(),
                user.getId().toString(), user.getRoles(), abateDate, SecurityUtils.FROM_PERMISS);
        user.setToken(token);
        return user;
    }

    /* *
     * @Author yyliu
     * @Description //TODO
     * @Date 10:10 2018/9/14
     * @Param [user]
     * @return void
     **/
    @Override
    public User doLogin(User user, String fromType){

        Subcompany subcompany = subcompanyLogic.findOneByCode(user.getCorpCode());
        Date abateDate = subcompany.getAbateDate();
        String token = SecurityUtils.encodeUserToken(user.getCorpCode(),
                user.getId().toString(), user.getRoles(), abateDate, fromType);
        user.setToken(token);
        userLogic.updateByPrimaryKeySelective(user);
        return user;
    }

    /* *
     * @Author yyliu
     * @Description //TODO
     * @Date 16:40 2018/9/14
     * @Param [corpCode, userId]
     * @return com.example.model.User
     **/
    @Override
    public User doLogin(String corpCode, Integer userId, String fromType){
        User user = userLogic.findOneById(userId);
        return doLogin(user, fromType);
    }
}
